﻿using AnesSystem.Controls;
using AnesSystem.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace AnesSystem.ViewModels
{
    public class PacuOperStatusItemCtrlViewModel : MultitonBaseViewModel
    {

        #region Variables
        private List<EventAndDrug> _eventList;
        private List<EventAndDrug> _drugList;
        private List<string> _operStatus;
        private Dictionary<string, string> _name2ctrlName;
        private ObservableCollection<FrameworkElement> _statusList;
        private List<double> _operStatusValueList;
        private static double EventStartY = 0;
        private static double DrugStartY = 20;
        private static double StatusStartY = 40;
        private static double NewColHeight = 10;
        private static string Event = "event";
        private static string Drug = "drug";
        private static string OperStatus = "operStatus";

        #endregion


        #region Constructor
        public PacuOperStatusItemCtrlViewModel(int page)
        {
            CurPage = page;
            InitOperStatus();
            _statusList = new ObservableCollection<FrameworkElement>();
        }


        #endregion


        #region Properties

        public List<EventAndDrug> EventList
        {
            get
            {
                return _eventList;
            }

            set
            {
                _eventList = value;
            }
        }

        public List<EventAndDrug> DrugList
        {
            get
            {
                return _drugList;
            }

            set
            {
                _drugList = value;
            }
        }

        public ObservableCollection<FrameworkElement> StatusList
        {
            get
            {
                return _statusList;
            }

            set
            {
                _statusList = value;
            }
        }

        #endregion


        #region 依赖属性


        #endregion


        #region Event


        #endregion


        #region PrivateMethods

        private void InitOperStatus()
        {
            _operStatus = new List<string>();
            _operStatus.Add("手术开始");
            _operStatus.Add("手术结束");
            _operStatus.Add("麻醉开始");
            _operStatus.Add("麻醉结束");
            _operStatus.Add("气管插管");
            _operStatus.Add("气管拔管");
            _name2ctrlName = new Dictionary<string, string>();
            _name2ctrlName.Add("手术", "SSKJCtrl");
            _name2ctrlName.Add("麻醉", "MZKJCtrl");
            _name2ctrlName.Add("气管", "QGCBGCtrl");
            _operStatusValueList = new List<double>();
        }
        /// <summary>
        /// 单次用药
        /// </summary>
        /// <param name="v"></param>
        /// <param name="beginTime"></param>
        private void AddOperDrugMark(string v, DateTime beginTime,bool isDown)
        {
            OperStatusTextCtrl ctrl = new OperStatusTextCtrl(v);
            ctrl.SetPoint(CalX(beginTime, ctrl), CalY(Drug, isDown));
            StatusList.Add(ctrl);
        }

        /// <summary>
        /// 手术事件
        /// </summary>
        /// <param name="v"></param>
        /// <param name="beginTime"></param>
        private void AddOperEventMark(string v, DateTime beginTime, bool isDown)
        {
            OperStatusTextCtrl ctrl = new OperStatusTextCtrl(v);
            ctrl.SetPoint(CalX(beginTime, ctrl),CalY(Event,isDown));
            StatusList.Add(ctrl);
        }

        /// <summary>
        /// 手术状态
        /// </summary>
        /// <param name="i"></param>
        /// <param name="beginTime"></param>
        private void AddOperStatusMark(string name, DateTime beginTime, bool isEnd)
        {
            string fullName = "";
            if (name.Contains("手术"))
            {
                fullName = "AnesSystem.Controls." + "SSKJCtrl";
            }
            else if (name.Contains("麻醉"))
            {
                fullName = "AnesSystem.Controls." + "MZKJCtrl";
            }
            else if (name.Contains("气管"))
            {
                fullName = "AnesSystem.Controls." + "QGCBGCtrl";
            }
            Type type = Type.GetType(fullName);
            if (type == null) return;
            EventIcon ctrl = Activator.CreateInstance(Type.GetType(fullName)) as EventIcon;
            if (!isEnd)
            {
                ctrl.Identify = true;
            }
            else
            {
                ctrl.Identify = false;
            }
            bool isDown = false;
            double x = CalX(beginTime, ctrl);
            foreach (var item in _operStatusValueList)
            {
                if (x-item<8 &&item-x<8)
                {
                    isDown = true;
                }
            }
            ctrl.SetPoint(x, CalY(OperStatus, isDown));
            _operStatusValueList.Add(x);
            StatusList.Add(ctrl);
        }

        private double CalY(string type, bool isDown)
        {
            if (type.Equals(Event))
            {
                if (isDown)
                {
                    return EventStartY + NewColHeight;
                }
                else
                {
                    return EventStartY;
                }
            }
            if (type.Equals(Drug))
            {
                if (isDown)
                {
                    return DrugStartY + NewColHeight;
                }
                else
                {
                    return DrugStartY;
                }
            }
            if (type.Equals(OperStatus))
            {
                if (isDown)
                {
                    return StatusStartY + NewColHeight;
                }
                else
                {
                    return StatusStartY;
                }
            }
            else
            {
                return 0;
            }

        }

        private double CalX(DateTime beginTime, EventIcon ctrl)
        {
            DateTime pacuRecordTime = DataManager.Instance.CurrentPatientLink.Status.PacuStartTime;
            DateTime startTime = pacuRecordTime.AddHours((CurPage - 1) * 4);
            TimeSpan span = beginTime - startTime;
            double span1 = span.Minutes + span.Hours * 60 + span.Days * 24 * 60;
            double x = -1;
            if (span1 != 0)
            {
                x = span1 / 5 * LocationUtil.RealTimeItem_Width-2;
            }
            else
            {
                x = 0;
            }

            return Convert.ToInt32(x);
        }
        #endregion


        #region Public/ProtectedMethod
        public override void InitData()
        {
            _eventList = new List<EventAndDrug>();
            _drugList = new List<EventAndDrug>();
            _eventList = (DataManager.Instance.PacuRecordCtrl.ViewModel as PacuRecordCtrlViewModel).PacuRecordCtrlViewModels[CurPage].PacuEventAndMedicineCtrlViewModel.EventList;
            _drugList = (DataManager.Instance.PacuRecordCtrl.ViewModel as PacuRecordCtrlViewModel).PacuRecordCtrlViewModels[CurPage].PacuEventAndMedicineCtrlViewModel.DrugList;
            StatusList.Clear();
            for (int i = 0; i < _eventList.Count; i++)
            {
                bool isBreak = false;
                if (!_operStatus.Contains(_eventList[i].Name))
                {
                    int startNo = i;
                    for (int j = (i + 1); j < _eventList.Count; j++)
                    {
                        if (!_operStatus.Contains(_eventList[j].Name))
                        {
                            if ((EventList[j].Time - _eventList[startNo].Time).TotalMinutes > 5)
                            {
                                break;
                            }
                            i++;
                            isBreak = true;
                            continue;
                        }
                        else
                        {
                            break;

                        }
                    }
                    if (i == startNo)
                    {
                        AddOperEventMark(_eventList[startNo].Index.ToString(), _eventList[startNo].Time, false);
                    }
                    if ((i - startNo) >= 2)
                    {
                        AddOperEventMark(_eventList[startNo].Index.ToString() + "-" + _eventList[i].Index.ToString(), _eventList[startNo].Time, false);
                    }
                    else
                    {
                        AddOperEventMark(_eventList[startNo].Index.ToString(), _eventList[startNo].Time, false);
                        if (isBreak)
                        {
                            AddOperEventMark(_eventList[startNo+1].Index.ToString(), _eventList[i].Time, true);
                        }
                    }
                }
                else
                {
                    if (_eventList[i].IsEnd || _eventList[i].PatientEvents.Code.Equals(Constants.EVENT_QIGUAN_BAGUAN))
                    {
                        AddOperStatusMark(_eventList[i].Name, _eventList[i].PatientEvents.EndTime, true);
                    }
                    else
                    {
                        AddOperStatusMark(_eventList[i].Name, _eventList[i].PatientEvents.BeginTime, false);
                    }

                }
            }

            for (int i = 0; i < _drugList.Count; i++)
            {
                int startNo = i;
                bool isBreak = false;
                for (int j = (i + 1); j < _drugList.Count; j++)
                {
                    if ((_drugList[j].PatientDrugs.BeginTime - _drugList[startNo].PatientDrugs.BeginTime).TotalMinutes > 5)
                    {
                        break;
                    }
                    i++;
                    isBreak = true;
                    continue;
                }
             
                if (i == startNo)
                {
                    AddOperDrugMark(_drugList[startNo].Index.ToString(), _drugList[startNo].Time, false);
                }
                if ((i - startNo) >= 2)
                {
                    AddOperDrugMark(_drugList[startNo].Index.ToString() + "-" + _drugList[i].Index.ToString(), _drugList[startNo].Time, false);
                }
                else
                {
                    AddOperDrugMark(_drugList[startNo].Index.ToString(), _drugList[startNo].Time, false);
                    if (isBreak)
                    {
                        AddOperDrugMark(_drugList[startNo+1].Index.ToString(), _drugList[i].Time, true);
                    }
                }
            }
        }



        public override void Refresh()
        {
            InitOperStatus();
            //           InitLine();
            InitData();
        }


        #endregion


        #region EventHandlers


        #endregion
    }
}
